<?php
namespace Android\Model;

class AttendModel extends BaseModel {


    public function addAttend($data) {

        if (empty($data))
            return false;

        return $this->add($data);
    }


    /**
     * 查找指定学生的出勤记录
     *
     * @param integer $student_id   学生id
     * @param integer $term_id      学期id
     */
    public function getAttendBySid($student_id,$term_id) {

        $result = $this->table("__ATTEND__ attend, __TEACHPLAN__ plan, __TIMETABLE__ kb")
                       ->where("attend.student_id = {$student_id} AND attend.term_id = {$term_id} AND attend.teachplan_id = plan.id AND attend.timetable_id = kb.id")
                       ->field("attend.status,attend.addtime,plan.teacher_id,plan.name AS teachplan_name ,plan.course_id,kb.week,kb.xq,kb.start,kb.end,kb.address")
                       ->order("addtime desc")
                       ->select();

        if (empty($result))
            return null;
        return $result;
    }


    /**
     * 获取特定的出勤记录
     *
     * @param $student_id
     * @param $status
     * @param $term_id
     * @return null
     */
    public function getSpecificAttend($student_id,$status,$term_id) {
        $result = $this->table("__ATTEND__ attend, __TEACHPLAN__ plan, __TIMETABLE__ kb")
                       ->field("attend.status,attend.addtime,plan.teacher_id,plan.name AS teachplan_name ,plan.course_id,kb.week,kb.xq,kb.start,kb.end,kb.address")
                       ->where("attend.student_id = {$student_id} AND attend.term_id = {$term_id} AND attend.status = {$status} AND attend.teachplan_id = plan.id AND attend.timetable_id = kb.id")
                       ->order("addtime desc")
                       ->select();

        if (empty($result))
            return null;
        return $result;
    }


    public function getAttendByTid($teachplan_id, $term_id) {
        $result = $this->table("__ATTEND__ attend, __TIMETABLE__ kb, __STUDENT__ student")
                       ->field("attend.id,attend.status,attend.addtime,kb.week,kb.xq,kb.start,kb.end,kb.address,student.name AS student_name, student.student_id, student.sex AS student_sex, student.photo,student.class_id")
                       ->where("attend.teachplan_id = {$teachplan_id} AND attend.term_id = {$term_id} AND attend.timetable_id = kb.id AND attend.student_id = student.id")
                       ->order("addtime desc")
                       ->select();
        if (empty($result))
            return null;
        return $result;
    }


    /**
     * @param $where
     */
    public function getCountByWhere($where) {
        return $this->where($where)->count();
    }
}